/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.commands;

import core.BoxNode;
import core.Node;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;
import model.graphic.objects.Attachment;
import model.graphic.objects.GConnection;
import model.graphic.objects.GBoxNode;
import model.graphic.objects.GNode;
import model.graphic.objects.GTransmitterNode;
import model.graphic.objects.Graphical;
import view.GUI.LabelReader;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Jelle
 */
public class DeleteCommand extends BasicCommand {

    private Graphical g = null;
    private boolean clearAll = false;

    public DeleteCommand() {
        clearAll = true;
    }

    public DeleteCommand(Graphical g) {
        this.g = g;
    }

    @Override
    public void execute() {
        if (!clearAll) {
            if (GNode.class.isAssignableFrom(g.getClass())) {
                GNode gn = (GNode) g;
                ArrayList<GConnection> overloop = new ArrayList<GConnection>();
                overloop.addAll(gn.getConns());
                for (GConnection gr1 : overloop) {
                    gview.notifyObservers(new DetachConnectionCommand(gr1));
                    gview.notifyObservers(new DeleteCommand(gr1));
                }
                if (!gn.isBoxed()) {
                    ArrayList<Graphical> overloop2 = new ArrayList<Graphical>();
                    overloop2.addAll(gmodel.getAttachedAllNode(gn));
                    for (Graphical gr2 : overloop2) {
                        gview.notifyObservers(new DeleteCommand(gr2));
                    }
                } else {
                    GBoxNode gb = gn.getBoxNode();
                    for (Graphical g : gb.getInternalObjects()) {
                        if (Attachment.class.isAssignableFrom(g.getClass())) {
                            Attachment port = (Attachment) g;
                            if (port.getBase() == gn) {
                                gview.remove(port);
                                for (Graphical g2 : gb.getInternalObjects()) {
                                    if (Attachment.class.isAssignableFrom(g2.getClass())) {
                                        Attachment handle = (Attachment) g;
                                        if (handle.getBase() == port) {
                                            gview.remove(handle);
                                        }
                                    }
                                }
                            }

                        }
                    }
                    gb.removeInternalNode(gn);
                }
                if (GTransmitterNode.class.isAssignableFrom(gn.getClass())) {
                    gmodel.removeDefObject(gn);
                    gview.remove(gn);
                } else {
                    gmodel.removeObject(gn);
                    gview.remove(gn);
                }
                Node n = sync.get(gn);
                cmodel.removeObject(n);
                sync.remove(n);
                controller.getDebug().append(LabelReader.getInstance().getString("NodeDeletedFromPanel"));
            } else if (GBoxNode.class.isAssignableFrom(g.getClass())) {
                GBoxNode gb = (GBoxNode) g;
                //delete zoom tab if it is open
                JTabbedPane viewtabs = controller.getViewtabs();
                for(int i = viewtabs.getTabCount() -1; i >= 0; i-- ){
                    if(((GraphicView)((JViewport)((JScrollPane)viewtabs.getComponentAt(i)).getViewport()).getView()).getBoxnodeParent() ==  gb){
                        viewtabs.remove(i);
                    }
                }

                ArrayList<Graphical> overloop = new ArrayList<Graphical>();
                overloop.addAll(gb.getTransmitterObjects());
                for (Graphical g1 : overloop) {
                    gview.remove(g1);
                    if (GTransmitterNode.class.isAssignableFrom(g1.getClass())) {
                        GTransmitterNode tn = (GTransmitterNode) g1;
                        ArrayList<GConnection> overloop2 = new ArrayList<GConnection>();
                        overloop2.addAll(tn.getConns());
                        for (GConnection gr1 : overloop2) {
                            gview.notifyObservers(new DetachConnectionCommand(gr1));
                            gview.notifyObservers(new DeleteCommand(gr1));
                        }
                    }
                }
                overloop.clear();
                overloop.addAll(gb.getInternalObjects());
                for (Graphical g2 : overloop) {
                    gview.remove(g2);
                }
                gview.remove(gmodel.getAttachedResize(gb));
                gmodel.removeObject(gmodel.getAttachedResize(gb));
                BoxNode bn = (BoxNode) sync.get(gb);
                gmodel.removeObject(gb);
                gview.remove(gb);
                cmodel.removeObject(bn);
                controller.getDebug().append(LabelReader.getInstance().getString("BoxnodeDeletedFromPanel"));
            } else {
                try {
                    gmodel.removeObject(g);
                } catch (Exception ex) {
                }
                try {
                    gmodel.removeDefObject(g);
                } catch (Exception ex) {
                }
                gview.remove(g);
            }
        } else {
            controller.getDebug().append(LabelReader.getInstance().getString("PanelIsCleared"));
            gview.removeAll();
            gmodel.removeAll();
            cmodel.removeAll();
        }

    }
}
